use rand::{distributions::{Standard, Uniform}, prelude::*};

 pub fn quick_start(){

    // 直接使用random(),按需返回随机数
    let x = random::<i8>();
    // let x:i8 = random();
    println!("随机数: {}", x);

    if random(){
        println!("随机布尔:真");
    }


    // 更复杂的thread_rng();
    let mut rng = rand::thread_rng(); // 可变
    if rng.gen() {
        // 随机布尔
        let x = rng.gen::<f64>(); // [0,1)之间
        let y:i32 = rng.gen();// i32 范围内随机数
        let z = rng.gen_range(-100.0..100.0);// [-100,100)之间
        
        println!(" [0,1) 随机：{:.2}",x);
        println!(" i32 范围随机：{}",y);
        println!(" [-100,100) 随机：{:.2}",z);
    }

    println!("摇骰子：{}",rng.gen_range(1..=6));
    

    // 使用特定的分布进行采样
    let uniform = rand::distributions::Uniform::new_inclusive(1,100 );
    // 1-100 均匀分布
    let mut nums = [0i32;3];
    for x in &mut nums{
        *x = rng.sample(uniform);
    }
    println!("三个均匀分布随机数:{:?}", nums);


    // 迭代器和切片灵活运用
    let emoji_iter = "😁😊😂🤣❤️😍😒👌😘💕👍🙌".chars();
    println!("emoji:{}",emoji_iter.choose(&mut rng).unwrap());

    let mut nums = [1,2,3,4,5];
    nums.shuffle(&mut rng); // 洗牌，打乱顺序
    println!("洗牌：{:?}",nums);
}


pub fn rand_custom_type(){
    let mut rng = thread_rng();
    let angle:Angle = rng.gen();
    println!("随机角度:{angle:?}");
}

use std::f64::consts::TAU; // =2Pi
#[derive(Debug)]
struct Angle(f64);

impl Angle {
    fn from_degrees(degrees:f64)->Self{
        Angle(degrees * 360.0/ TAU)
    }
}

impl Distribution<Angle> for Standard{
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Angle {
        Angle::from_degrees(Uniform::new(0.0, TAU).sample(rng))
    }
}